package com.example.farmeri.ui.farm_data;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.example.farmeri.R;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.data.config.ServiceConfig;
import com.example.farmeri.http.API;
import com.example.farmeri.pojo.Farm;
import com.example.farmeri.pojo.vo.BaseCallBack;
import com.example.farmeri.tools.OKHttpClientUtils;
import com.example.farmeri.ui.base.BaseActivity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 农田数据详情
 */
public class FarmDataEditActivity extends BaseActivity {

    private AlertDialog dialog;

//    @BindView(R.id.farm_data_edit_img)
//    ImageView img;
    @BindView(R.id.farm_data_edit_name)
    EditText farm_name;
    @BindView(R.id.farm_data_edit_location)
    EditText farm_location;
    @BindView(R.id.farm_data_edit_area)
    EditText farm_area;
    @BindView(R.id.farm_data_edit_remarks)
    EditText farm_remarks;
    @BindView(R.id.farm_data_edit_temperature)
    EditText farm_temperature;
    @BindView(R.id.farm_data_edit_humidity)
    EditText farm_humidity;
    @BindView(R.id.farm_data_edit_sun)
    EditText farm_sun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farm_data_edit);
        ButterKnife.bind(this);
        init();
        fetchData();
    }

    private void fetchData() {
        Farm farm = getIntent().getParcelableExtra("farm");
        if (farm!= null){
            fillData(farm);
        }
    }

    private void fillData(Farm farm) {
        farm_area.setText(farm.getSize());
        farm_humidity.setText(farm.getHumidity());
        farm_temperature.setText(farm.getTemperature());
        farm_sun.setText(farm.getIllumination());
        farm_location.setText(farm.getLocation());
        farm_name.setText(farm.getName());
        farm_remarks.setText(farm.getRemarks());
    }

    private void init() {
        initDialog();
        initToolBar(R.id.farm_data_edit_toolbar,"编辑",true,true);

    }

    private void initDialog() {
        dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage("修改成功");
        dialog.setTitle("提示");
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
    }

    // 修改提交
    public void btn_alert(View view) {
        String id = DataParam.getUserInfo().getId();
        if (id.equals("") || null==id){
            showToast(this,"上传信息失败");
            return;
        }
        Farm farm = getIntent().getParcelableExtra("farm");
        if (farm == null){
            showToast("提交失败");
        }
        Map<String ,String> map = new HashMap<>();
        map.put("farmId", String.valueOf(farm.getId()));
        map.put("name",farm_name.getText().toString());
        map.put("location",farm_location.getText().toString());
        map.put("size",farm_area.getText().toString());
        map.put("temperature",farm_temperature.getText().toString());
        map.put("humidity",farm_humidity.getText().toString());
        map.put("illumination",farm_sun.getText().toString());
        map.put("remarks",farm_remarks.getText().toString());
        map.put("byUid",id);
        OKHttpClientUtils.userPost(ServiceConfig.editFarmData(),OKHttpClientUtils.TYPE_FORM,map,new BaseCallBack(handler,TYPE_EDIT));
    }

    final Integer TYPE_EDIT = 0x100;
    final Integer TYPE_ERROR = 0x400;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == TYPE_EDIT){
                dialog.show();
            }

            if (msg.what == TYPE_ERROR){
                showToast(FarmDataEditActivity.this,"网络请求失败");
                return;
            }


        }
    };



    public void updataPic(View view) {

    }

    final static Integer ACTION_CAMERA = 1;         //使用相机
    final static Integer ACTION_ALBUM = 2;         //使用相册

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ACTION_CAMERA) {
                Bundle extras = data.getExtras();
                Log.d("Image", "data:" + data);
                Bitmap b = (Bitmap) extras.get("data");
//                img.setImageBitmap(b);
            } else if (requestCode == ACTION_ALBUM) {
                Uri uri = data.getData();
                Log.d("Image", "data:" + data);
//                img.setImageURI(uri);
                //ContentResolver cr = this.getContentResolver();
                //Cursor c = cr.query(uri,null,null,null,null);
                //c.moveToFirst();
                //File file = new File(uri);
            }

            //Integer id;
            //service.updataImg(id,img);
        }

    }
}
